<?php

//公共控制器
class CommonController extends Controller{

	//检测用户名是否存在
	public function checkUser(){
		if(!IS_AJAX) $this->error('非法请求');
		//用户名
		$username = Q('post.username');
		//如果有数据，证明用户名存在
		if(K('User')->where("username='{$username}'")->find()){
			$this->ajax(array('message'=>'用户名已存在','status'=>false));
		}else{
			$this->ajax(array('message'=>'ok!','status'=>true));
		}
	}
	//检测用户邮箱是否存在
	public function checkEmail(){
		if(!IS_AJAX) $this->error('非法请求');
		//用户邮箱
		$email = Q('post.email');
		//如果有数据，证明用户名存在
		if(K('User')->where("email='{$email}'")->find()){
			$this->ajax(array('message'=>'该邮箱已注册','status'=>false));
		}else{
			$this->ajax(array('message'=>'ok!','status'=>true));
		}
	}
	//验证码
	public function code(){
		$code= new Code();
		$code->show();
	}
	
	//检测验证码是否正确
	public function checkCode(){
		if(!IS_AJAX) $this->error('非法请求');
		//验证码
		$code = strtoupper(Q('post.code'));
		//判断验证码
		if($code != $_SESSION['code']){
			$this->ajax(array('message'=>'请填写正确的验证码','status'=>false));
		}else{
			$this->ajax(array('message'=>'ok!','status'=>true));
		}
	}
	
	//注册
	public function register(){
		if(IS_POST){
			//获得提交表单内容
			$username=Q('post.username_reg');
			$passwork=md5(Q('post.password_reg1'));
			$nickname=Q('post.nickname');
			//组合数组
			$data=array(
			   'username' => $username,
			   'nickname' => $nickname,
			   'password' => $passwork
			);
			//往用户表中追加数据
			K('user')->add($data);
			
			$this->success('注册成功');
			
		}
		
	}
	
	//登陆
	public function login(){
		if(IS_POST){
			//获得提交表单内容
			$username=Q('post.username');
			$passwork=md5(Q('post.password'));
			if(!K('user')->where("username='{$username}'")->find()){
				$this->error('用户名不存在');
			}
			if(!K('user')->where("username='{$username}' AND password='{$passwork}'")->find()){
				$this->error('密码错误');
			}
			$data=K('user')->where("username='{$username}'")->find();
		
			//存入session
			$_SESSION['uid']=$data['uid'];
			$_SESSION['username']=$data['username'];
			
			$this->success('登陆成功');
			
		}
		
	}
	
	//退出
	public function out(){
		//退出SESSION
		session(null);
		$this->success('退出成功');
	}
	
}



?>